import CFG from "./config.js"
var common = require('./util.js')
function getBaseUrl(){
	let baseUrl = CFG.APP_API_PRO_HOST
	if(CFG.ENV == 'dev'){
		baseUrl = CFG.APP_API_HOST
	}else if(CFG.ENV == 'pre'){
		baseUrl = CFG.APP_API_PRE_HOST
	}else if(CFG.ENV == 'prod'){
		baseUrl = CFG.APP_API_PRO_HOST
	}
	return baseUrl
}
function request(url, method, data, loading){
	let header = {
		"Content-Type": "application/json; charset=UTF-8",
		token: '123456',
		lang: 'zh-CN'
	}
	if(data && data.header){
		for(let key in data.header){
			header[key] = data.header[key]
		}
		delete data.header
	}
	data.deviceType = CFG.DEVICE_TYPE
	data = common.desEncrypt(data)
	return new Promise((resolve, reject) => {
		uni.request({
			url: getBaseUrl() + url,
			data: data,
			header: header,
			method: method,
			dataType: 'json',
			success: (res) => {
				resolve(res.data && res.data.data ? res.data.data : res.data, res.data ? res.data : res)
			},
			fail: (res) => {
				reject(res.data && res.data.data ? res.data.data : res.data, res.data ? res.data : res)
			}
		})
	})
}
function get(url, data, loading){
	request(url, 'GET', data, loading)
}
function post(url, data, loading){
	request(url, 'POST', data, loading)
}
function postForm(){}
export {
	request,
	get,
	post,
	postForm
}
